package org.jeecg.basic.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.basic.dto.SysExceptionHistoryDTO;
import org.jeecg.basic.entity.MesRoutingCfg;
import org.jeecg.basic.entity.SysExceptionHistory;
import org.jeecg.basic.service.SysExceptionHistoryService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags="系统异常历史")
@RestController
@RequestMapping("/basic/sysExceptionHistory")
@Slf4j
public class SysExceptionHistoryController extends JeecgController<SysExceptionHistory, SysExceptionHistoryService> {

    @Autowired
    private SysExceptionHistoryService sysExceptionHistoryService;

    @Value("${jeecg.path.upload}")
    private String upLoad;

    /**
     * 分页列表查询
     *
     * @param dto
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "系统异常历史记录-分页列表查询")
    @ApiOperation(value="系统异常历史记录-分页列表查询", notes="系统异常历史记录-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<SysExceptionHistory>> queryPageList(SysExceptionHistoryDTO dto,
                                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                            HttpServletRequest req) {
        SysExceptionHistory his = new SysExceptionHistory();
        his.setRecId(dto.getRecId())
                .setLine(dto.getLine())
                .setServer(dto.getServer())
                .setOp(dto.getOp()).setStn(dto.getStn())
                .setCmd(dto.getCmd()).setWoid(dto.getWoid())
                .setUnitsn(dto.getUnitsn()).setCtime(dto.getCtime())
                .setErrcode(dto.getErrcode()).setErrmsg(dto.getErrmsg());
        QueryWrapper<SysExceptionHistory> queryWrapper = QueryGenerator.initQueryWrapper(his, req.getParameterMap());
        if (dto.getSTime() != null && dto.getETime() != null) {
            queryWrapper.between("CTIME", dto.getSTime(), dto.getETime());
        }
        queryWrapper.orderByDesc("CTIME");
        Page<SysExceptionHistory> page = new Page<SysExceptionHistory>(pageNo, pageSize);
        IPage<SysExceptionHistory> pageList = sysExceptionHistoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param dto
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_routing_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, SysExceptionHistoryDTO dto) {
//        return super.exportXls(request, sysExceptionHistory, SysExceptionHistory.class, "系统异常历史记录");
        // Step.1 组装查询条件
        SysExceptionHistory his = new SysExceptionHistory();
        his.setRecId(dto.getRecId())
                .setLine(dto.getLine())
                .setServer(dto.getServer())
                .setOp(dto.getOp()).setStn(dto.getStn())
                .setCmd(dto.getCmd()).setWoid(dto.getWoid())
                .setUnitsn(dto.getUnitsn()).setCtime(dto.getCtime())
                .setErrcode(dto.getErrcode()).setErrmsg(dto.getErrmsg());
        QueryWrapper<SysExceptionHistory> queryWrapper = QueryGenerator.initQueryWrapper(his, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            queryWrapper.in("id", selectionList);
        }
        if (dto.getSTime() != null && dto.getETime() != null) {
            queryWrapper.between("CTIME", dto.getSTime(), dto.getETime());
        }
        // Step.2 获取导出数据
        List<SysExceptionHistory> exportList = service.list(queryWrapper);

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "系统异常历史记录");
        mv.addObject(NormalExcelConstants.CLASS, SysExceptionHistory.class);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("系统异常历史记录" + "报表", "导出人:" + sysUser.getRealname(), "系统异常历史记录");
        exportParams.setImageBasePath(upLoad);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("mes_routing_cfg:importExcel")
    @DS("#header.line")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysExceptionHistory.class);
    }
}
